#ifndef __RC2412_TMRA_H__
#define __RC2412_TMRA_H__

#ifdef __cplusplus
extern "C"
{
#endif

#include "../../periph_def/rc2412_timerA_def.h"

typedef enum 
{
  TACLK       = 0x0ul,     
  ACLK        = 0x1ul,
	SMCLK       = 0x2ul,
	TACLK_O     = 0x3ul,
}timer_clk_src_t;

typedef enum 
{
  TIM_STOP    = (0x0ul<<TMRA_MC_Pos),     
  TIM_UP      = (0x1ul<<TMRA_MC_Pos),
	TIM_UP_HEAD = (0x2ul<<TMRA_MC_Pos),
	TIM_UP_DOWN = (0x3ul<<TMRA_MC_Pos),
	TIM_MAX
}timer_mc_t;


typedef struct 
{
	  timer_mc_t     mc;
    uint32_t     count;             /*!< Timer counter value, different working modes have different values */
    void        (*callback)(void);  /*!< The callback of timer interrupt */
}timer_config_t;

/** timer pwm output polarity */
typedef enum 
{
  TIM_PWM_POL_LOW   = (uint32_t)(0x0ul<<TMRA_OUT_Pos),     //PWM set high on compare macth,set low  at the end of PWM period
  TIM_PWM_POL_HIGH  = (uint32_t)(0x1ul<<TMRA_OUT_Pos)  //PWM set low  on compare macth,set high at the end of PWM period
}pwm_pol_t;

typedef enum 
{
    OUT_BIT_VAL     = (0x0ul<<TMRA_OUTMOD_Pos),
	  OUT_SET         = (0x1ul<<TMRA_OUTMOD_Pos),
	  OUT_TOGGLE_RES  = (0x2ul<<TMRA_OUTMOD_Pos),
	  OUT_SET_RES     = (0x3ul<<TMRA_OUTMOD_Pos),
	  OUT_TOGGLE      = (0x4ul<<TMRA_OUTMOD_Pos),
	  OUT_RES         = (0x5ul<<TMRA_OUTMOD_Pos),
	  OUT_TOGGLE_SET  = (0x6ul<<TMRA_OUTMOD_Pos),
	  OUT_RES_SET     = (0x7ul<<TMRA_OUTMOD_Pos),
	  OUT_MAX
}pwm_mod_t;

typedef struct
{
  //uint16_t      pwm_prescal;  //PWM's Prescaler value,10 bit value  
  uint32_t      pwm_period;   //PWM's Period
  //uint32_t      pwm_pulse;    //PWM's Pulse
	timer_mc_t      mc;
	pwm_mod_t     pwm_mod;
  pwm_pol_t     pwm_polarity; //PWM's Polarity before wave reverse
}pwm_config_t;


/** Input capture edge type */
typedef enum
{
    CAP_NO            = (0x0ul << TMRA_CM_Pos),
    CAP_EDGE_RISING   = (0x1ul << TMRA_CM_Pos), /*!< Rising edge is set as input capture edge */
    CAP_EDGE_FALLING  = (0x2ul << TMRA_CM_Pos), /*!< Falling edge is set as input capture edge */
    CAP_EDGE_BOTH     = (0x3ul << TMRA_CM_Pos) /*!< Both rising and falling edge are set as input capture edge */ 
}tim_cap_edge_t;

/** Input capture edge type */
typedef enum
{
    CCIS_CCIxA = (0x0ul << TMRA_CCIS_Pos),
    CCIS_ICLK  = (0x1ul << TMRA_CCIS_Pos), /*!< Rising edge is set as input capture edge */
    CCIS_LRC   = (0x2ul << TMRA_CCIS_Pos), /*!< Falling edge is set as input capture edge */
    CCIS_GND   = (0x3ul << TMRA_CCIS_Pos) /*!< Both rising and falling edge are set as input capture edge */ 
}tim_cap_ccis_t;


typedef enum
{
    COMPARE_MODE      = (0x0ul << TMRA_CAP_Pos),
    CAPTURE_MODE      = (0x1ul << TMRA_CAP_Pos), /*!< Rising edge is set as input capture edge */
}tim_cap_mode_t;


typedef struct
{
  tim_cap_mode_t      cap_mode;      //Input Capture/Compare Mode
  tim_cap_edge_t      cap_edge;      //Input Capture Edge
	tim_cap_ccis_t      cap_input;
  //uint16_t            cap_prescal;   //Input capture Prescal value
	timer_mc_t          mc;
  uint32_t            cap_count;     //Input Capture Event Count,only valid in input capture count mode
//  uint32_t            cap_evt_period;//Input Capture Event Period,only valid in input capture event mode
	void        (*callback)(void);  /*!< The callback of timer interrupt */
}cap_config_t;

typedef enum
{
	TIMERA0      = 0,
	TIMERA1      = 1,
	TIMERA2      = 2,
	TIMERA3      = 3,
	TIMERA4      = 4,
	TIMERA5      = 5,
	TIMERA6      = 6,
	TIMERA7      = 7,
	TIMERA_MAX
}dma_timer_config_t;



extern volatile uint32_t cap_int_count;


void timerA_init(void);
void timerA_reset(void);
void timerA_timer_cfg(timer_config_t *config);
void timerA_pwm_cfg(pwm_config_t *config);
void timerA_cap_cfg(cap_config_t *config);
void timerA_compare_cfg(cap_config_t *config);


#ifdef __cplusplus
}
#endif

#endif // __TMRA_H__
